<?php

class App_Methods_Request_SetGroupApplication {

  /**
   *
   * @var App_Methods_Data_App
   */
  public $app;
  public $userGroupId;
  public $applicationId;
  public $action;

  public function __construct($params) {
    $this->app = new App_Methods_Data_App($params->app);
    $this->userGroupId = $params->userGroupId;
    $this->applicationId = $params->applicationId;
    $this->action = $params->action;
  }

  public function process() {
    if (!Core_Auth::getInstance()->getUser()->isAdmin())
    	throw new App_Exception_App(4000);
    
    if ($this->action == 'add') {
    	$userGroupApplications = new UserGroupApplications();
    	$userGroupApplications->setApplicationId($this->applicationId);
    	$userGroupApplications->setUserGroupId($this->userGroupId);
    	$userGroupApplications->save();
    }
    else if ($this->action == 'remove') {
    	UserGroupApplicationsQuery::create()
    	->filterByApplicationId($this->applicationId)
    	->filterByUserGroupId($this->userGroupId)
    	->delete();
    }
    else
    	throw new App_Exception_App(4000);
    
    return new App_Methods_Response_SetGroupApplication(true);
  }

}